package sms;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/SendSMSServlet")
public class SendSMSServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        //2.获取请求参数
        String phone=request.getParameter("phone");

        //3.进行业务处理 1000  9999  100,1000  1000 10000
        //Math.random()*(1000-100)+100
        //生成随机验证码
        int code=(int)(Math.random()*(10000-1000)+1000);
        //保存验证码到session
        request.getSession().setAttribute("code",code+"");
        //发送短信验证码到手机
        boolean result= SMSUtil.send(phone,code+"");

        //4.响应返回结果
        response.getWriter().print(result);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
